<?php

/*============================================
Classe McsSubsystem
============================================*/

class McsSubsystem extends ModelObject{

	protected static $cached_object_list= array();
	protected $SUBSYSTEM_NAME;
	protected $SUBSYSTEM_DESCR;
	protected $SUBSYSTEM_TO_LOCK;

	protected function __construct( $SUBSYSTEM_NAME,$SUBSYSTEM_DESCR,$SUBSYSTEM_TO_LOCK){
		try{
			
			$this->SUBSYSTEM_NAME = $SUBSYSTEM_NAME;
			$this->SUBSYSTEM_DESCR = $SUBSYSTEM_DESCR;
			$this->SUBSYSTEM_TO_LOCK = $SUBSYSTEM_TO_LOCK;
		}
		catch(Excpetion $e){
			throw new Exception("La construction de l'objet de type get_class($this) a échouée",0,$e);
		}
	}

	public function getSUBSYSTEMNAME(){
		return $this->SUBSYSTEM_NAME;
	}

	public function getSUBSYSTEMDESCR(){
		return $this->SUBSYSTEM_DESCR;
	}

	public function getSUBSYSTEMTOLOCK(){
		return $this->SUBSYSTEM_TO_LOCK;
	}

	public function setSUBSYSTEMDESCR( $SUBSYSTEM_DESCR){
		try{
			$temp = $this->SUBSYSTEM_DESCR;
			if($this->SUBSYSTEM_DESCR !== $SUBSYSTEM_DESCR){
				$this->SUBSYSTEM_DESCR = $SUBSYSTEM_DESCR;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->SUBSYSTEM_DESCR = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setSUBSYSTEMDESCR() a échouée",0,$e);
		}
	}

	public function setSUBSYSTEMTOLOCK( $SUBSYSTEM_TO_LOCK){
		try{
			$temp = $this->SUBSYSTEM_TO_LOCK;
			if($this->SUBSYSTEM_TO_LOCK !== $SUBSYSTEM_TO_LOCK){
				$this->SUBSYSTEM_TO_LOCK = $SUBSYSTEM_TO_LOCK;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->SUBSYSTEM_TO_LOCK = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setSUBSYSTEMTOLOCK() a échouée",0,$e);
		}
	}

	public static function create( $SUBSYSTEM_NAME,$SUBSYSTEM_DESCR,$SUBSYSTEM_TO_LOCK){
		try{
			$mcsSubsystem = new McsSubsystem($SUBSYSTEM_NAME, $SUBSYSTEM_DESCR, $SUBSYSTEM_TO_LOCK);
			$mcsSubsystem->insert();
			McsSubsystem::cacheObject($mcsSubsystem);
			return $mcsSubsystem;
		}
		catch(Excpetion $e){
			throw new Exception("La création de l'objet de type McsSubsystem a échouée",0,$e);
		}
	}

	public static function get( $SUBSYSTEM_NAME){
		$dbh = SingletonPDO::getInstance();
		try{
			if(is_null($SUBSYSTEM_NAME) || $SUBSYSTEM_NAME === "") {
				return null;
			}
			if(McsSubsystem::existsInCache($SUBSYSTEM_NAME)) {
				return McsSubsystem::getFromCache($SUBSYSTEM_NAME);
			}
			$dbh->beginTransaction();
			$sql= 'SELECT * FROM `mcs_subsystem` WHERE `SUBSYSTEM_NAME` = :id';
			$stmt = $dbh->prepare($sql);
			$stmt->execute(array(':id'=>$SUBSYSTEM_NAME));
			if ($row = $stmt->fetch()){
				$mcsSubsystem = new McsSubsystem( $row['SUBSYSTEM_NAME'],$row['SUBSYSTEM_DESCR'],$row['SUBSYSTEM_TO_LOCK']);
				$mcsSubsystem->id =  $row['SUBSYSTEM_NAME'];
				$dbh->commit();
				McsSubsystem::cacheObject($mcsSubsystem);
				return $mcsSubsystem;
			}
			else {
				$dbh->commit();
				return null;
			}
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La récupération de l'objet ayant l'id ". $SUBSYSTEM_NAME . " a échouée",0,$e);
		}
	}

	protected function update(){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			if (is_null($this->id)) {
				throw new Exception("Cet objet de type ".get_class($this)." n'a pas encore été enregistré dans la base");
			}
			$sql='UPDATE `mcs_subsystem` SET `SUBSYSTEM_DESCR` = :SUBSYSTEM_DESCR, `SUBSYSTEM_TO_LOCK` = :SUBSYSTEM_TO_LOCK WHERE `SUBSYSTEM_NAME` = :id';
			$stmt = $dbh->prepare($sql);
			$stmt->execute(array(':SUBSYSTEM_DESCR'=>$this->SUBSYSTEM_DESCR, ':SUBSYSTEM_TO_LOCK'=>$this->SUBSYSTEM_TO_LOCK, ':id'=>$this->id));
			$dbh->commit();
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La modification de l'objet de type ".get_class($this)." ayant l'id ". $this->id . " a échouée",0,$e);
		}
	}

	public function delete(){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			if (is_null($this->id)) {
				throw new Exception("Cet objet de type ".get_class($this)." n'a pas encore été enregistré dans la base");
			}
			$sql = 'DELETE FROM `mcs_subsystem` WHERE `SUBSYSTEM_NAME` = :id';
			$stmt = $dbh->prepare($sql);
			$stmt->execute(array(':id'=>$this->id));
			McsSubsystem::removeObjectFromCache($this->id);
			$this->id = null;
			$dbh->commit();
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La suppression de l'objet de type ".get_class($this)." ayant l'id ". $this->id. " a échouée",0,$e);
		}
	}

	protected function insert(){
		$dbh = SingletonPDO::getInstance();
		try{
			if (is_null($this->id)) {
				$dbh->beginTransaction();
				$sql='INSERT INTO `mcs_subsystem` ( `SUBSYSTEM_DESCR`, `SUBSYSTEM_TO_LOCK`) VALUES (:SUBSYSTEM_DESCR, :SUBSYSTEM_TO_LOCK)';
				$stmt = $dbh->prepare($sql);
				$stmt->execute(array(':SUBSYSTEM_DESCR'=>$this->SUBSYSTEM_DESCR, ':SUBSYSTEM_TO_LOCK'=>$this->SUBSYSTEM_TO_LOCK));
				$this->id = $dbh->lastInsertId();
				$dbh->commit();
			}
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("L'insertion de l'objet de type ".get_class($this)." a échouée",0,$e);
		}
	}

	public static function getAll( $search_params = array(),$options = array()){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			$objs = array();
			$sql = 'SELECT * FROM `mcs_subsystem`';
			$sql .= self::generateWhereClause($search_params);
			$sql .= self::generateOptionClause($options);
			$stmt = $dbh->prepare($sql);
			$array = array();
			$cpt = 0;
			foreach($search_params as $ind => $search_param){
				//ajouter un parametre au tableau
				if($search_param['operator'] === 'BETWEEN' && is_array($search_param['value']) && count($search_param['value']) == 2 ){
					$array[':'.$search_param['field'].$cpt.'_0'] = $search_param['value'][0];
					$array[':'.$search_param['field'].$cpt.'_1'] = $search_param['value'][1];
				}
				else if($search_param['operator'] === 'IN' && is_array($search_param['value']) && count($search_param['value']) > 0){
					$i = 0;
					foreach($search_param['value'] as $key => $val){
						$array[':'.$search_param['field'].$cpt.'_'.$i] = $val;
						$i++;
					}
					if($i == 0){
						throw new Exception("L'opérateur IN doit comporter au moins une valeur");
					}
				}
				else if ($search_param['operator'] === 'IS NULL' || $search_param['operator'] === 'IS NOT NULL'){
					continue;
				}
				else{
					$array[':'.$search_param['field'].$cpt] = $search_param['value'];
				}
				$cpt++;
			}
			$stmt->execute($array);
			while ($row = $stmt->fetch()) {
				$obj = null;
				if(McsSubsystem::existsInCache($row['SUBSYSTEM_NAME'])){
					$obj = McsSubsystem::getFromCache($row['SUBSYSTEM_NAME']);
				}
				else{
					$obj = new McsSubsystem( $row['SUBSYSTEM_NAME'],$row['SUBSYSTEM_DESCR'],$row['SUBSYSTEM_TO_LOCK']);
				}
				if(!is_null($obj)){
					$obj->id = $row['SUBSYSTEM_NAME'];
					McsSubsystem::cacheObject($obj);
					$objs[] = $obj;
				}
			}
			$dbh->commit();
			return $objs;
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La récuperation de la liste d'objets de type McsSubsystem a échouée",0,$e);
		}
	}

	public static function getCount( $search_params = array(),$options = array()){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			$objs = array();
			$sql = 'SELECT count(*) as count FROM `mcs_subsystem`';
			$sql .= self::generateWhereClause($search_params);
			$sql .= self::generateOptionClause($options);
			$stmt = $dbh->prepare($sql);
			$array = array();
			$cpt = 0;
			foreach($search_params as $ind => $search_param){
				//ajouter un parametre au tableau
				if($search_param['operator'] === 'BETWEEN' && is_array($search_param['value']) && count($search_param['value']) == 2 ){
					$array[':'.$search_param['field'].$cpt.'_0'] = $search_param['value'][0];
					$array[':'.$search_param['field'].$cpt.'_1'] = $search_param['value'][1];
				}
				else if($search_param['operator'] === 'IN' && is_array($search_param['value']) && count($search_param['value']) > 0){
					$i = 0;
					foreach($search_param['value'] as $key => $val){
						$array[':'.$search_param['field'].$cpt.'_'.$i] = $val;
						$i++;
					}
					if($i == 0){
						throw new Exception("L'opérateur IN doit comporter au moins une valeur");
					}
				}
				else if ($search_param['operator'] === 'IS NULL' || $search_param['operator'] === 'IS NOT NULL'){
					continue;
				}
				else{
					$array[':'.$search_param['field'].$cpt] = $search_param['value'];
				}
				$cpt++;
			}
			$stmt->execute($array);
			$count = $stmt->fetch();
			$dbh->commit();
			return $count['count'];
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La récuperation de la liste d'objets de type McsSubsystem a échouée",0,$e);
		}
	}

	public static function deleteAll( $search_params = array()){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			$objs = array();
			$sql_delete = 'DELETE FROM `mcs_subsystem`';
			$sql_select = 'SELECT SUBSYSTEM_NAME FROM `mcs_subsystem`';
			$sql_delete .= self::generateWhereClause($search_params);
			$sql_select .= self::generateWhereClause($search_params);
			$stmt_delete = $dbh->prepare($sql_delete);
			$stmt_select = $dbh->prepare($sql_select);
			$array = array();
			$cpt = 0;
			foreach($search_params as $ind => $search_param){
				//ajouter un parametre au tableau
				if($search_param['operator'] === 'BETWEEN' && is_array($search_param['value']) && count($search_param['value']) == 2 ){
					$array[':'.$search_param['field'].$cpt.'_0'] = $search_param['value'][0];
					$array[':'.$search_param['field'].$cpt.'_1'] = $search_param['value'][1];
				}
				else if($search_param['operator'] === 'IN' && is_array($search_param['value']) && count($search_param['value']) > 0){
					$i = 0;
					foreach($search_param['value'] as $key => $val){
						$array[':'.$search_param['field'].$cpt.'_'.$i] = $val;
						$i++;
					}
					if($i == 0){
						throw new Exception("L'opérateur IN doit comporter au moins une valeur");
					}
				}
				else if ($search_param['operator'] === 'IS NULL' || $search_param['operator'] === 'IS NOT NULL'){
					continue;
				}
				else{
					$array[':'.$search_param['field'].$cpt] = $search_param['value'];
				}
				$cpt++;
			}
			$var_select = $stmt_select->execute($array);
			$tab_to_delete_objects = $stmt_select->fetchAll();
			if(sizeof($tab_to_delete_objects) > 0){
				$var_delete = $stmt_delete->execute($array);
				foreach($tab_to_delete_objects as $ind => $row){
					$existing_object = McsSubsystem::getObjectFromCache($row['SUBSYSTEM_NAME']);
					if(!is_null($existing_object)){
						$existing_object->id = null;
						McsSubsystem::removeObjectFromCache($row['SUBSYSTEM_NAME']);
					}
				}
			}
			$dbh->commit();
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La suppression de la liste d'objets de type McsSubsystem a échouée",0,$e);
		}
	}

}